package com.yunkaweilai.android.activity.member;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.interfaces.StringInterface;
import com.yunkaweilai.android.model.member.MemberLevelModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.dialog.DelMemberLevelDialog;
import com.yunkaweilai.android.view.dialog.NoTimeDialog;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页会员等级
 * Created by Administrator on 2018/6/27.
 */

public class MemberHomeLevelActivity extends BaseActivity implements StringInterface {

    @BindView(R.id.titlebar_iv_left)
    ImageView titlebarIvLeft;
    @BindView(R.id.titlebar_tv_left)
    TextView titlebarTvLeft;
    @BindView(R.id.titlebar_tv)
    TextView titlebarTv;
    @BindView(R.id.titlebar_iv_right_2)
    ImageView titlebarIvRight2;
    @BindView(R.id.titlebar_iv_right)
    ImageView titlebarIvRight;
    @BindView(R.id.titlebar_tv_right)
    TextView titlebarTvRight;
    @BindView(R.id.id_view_underline)
    View idViewUnderline;
    @BindView(R.id.rl_titlebar)
    RelativeLayout rlTitlebar;
    @BindView(R.id.content_view)
    ListView contentView;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultipleStatusView;
    @BindView(R.id.id_empty_img)
    ImageView idEmptyImg;
    @BindView(R.id.id_empty_layout)
    RelativeLayout idEmptyLayout;
    private CommonAdapter<MemberLevelModel.DataBean.ListBean> adapter;
    private ArrayList<MemberLevelModel.DataBean.ListBean> list = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.member_home_level_layout);
        ButterKnife.bind(this);
        idMultipleStatusView.showLoading();
        idMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initHttp();
            }
        });
        new TitleBuilder(mAc).setLeftImage(R.mipmap.ic_go_back).setTitleText("会员等级")
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                }).setRightText("添加").setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(mAc, AddMemberLevelActivity.class), 101);
//                intent2Activity(AddMemberLevelActivity.class);
            }
        });

        initAdapter();
        initHttp();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 101) {
                showProgressDialog();
                initHttp();
            } else if (requestCode == 102) {
                showProgressDialog();
                initHttp();
            }
        }
    }

    private void initAdapter() {
        adapter = new CommonAdapter<MemberLevelModel.DataBean.ListBean>(mAc, R.layout.item_member_level_list, list) {
            @Override
            protected void convert(ViewHolder viewHolder, final MemberLevelModel.DataBean.ListBean item, int position) {
                TextView idTvName = viewHolder.getView(R.id.id_tv_name);
                idTvName.setText(Html.fromHtml("<font color='#555555'>" + item.getLevel_name() + "</font>   <font color='#999999'>(" + item.getLevel_member_count() + ")</font>"));
//                viewHolder.setText(R.id.id_tv_name, item.getLevel_name());
                TextView idtvdel = viewHolder.getView(R.id.id_tv_del);
                TextView idtvupdate = viewHolder.getView(R.id.id_tv_update);
                if (item.isIs_del()) {
                    idtvdel.setTextColor(getResources().getColor(R.color.wjx_content_txt));
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        idtvdel.setBackgroundResource(R.drawable.bg_black_stroke);
                    }
                } else {
                    idtvdel.setTextColor(getResources().getColor(R.color.wjx_content_txt_grey));
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        idtvdel.setBackgroundResource(R.drawable.bg_grey_stroke);
                    }
                }
                if (item.isIs_edit()) {
                    idtvupdate.setTextColor(getResources().getColor(R.color.wjx_title_bg_next));
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        idtvupdate.setBackgroundResource(R.drawable.bg_green_stroke);
                    }
                } else {
                    idtvupdate.setTextColor(getResources().getColor(R.color.wjx_content_txt_grey));
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        idtvupdate.setBackgroundResource(R.drawable.bg_grey_stroke);
                    }
                }
                idtvdel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (item.isIs_del()) {
                            DelMemberLevelDialog delMemberLevelDialog = new DelMemberLevelDialog(mAc, "确定要删除吗？", item.getId(), MemberHomeLevelActivity.this);
                            delMemberLevelDialog.setCanceledOnTouchOutside(false);
                            delMemberLevelDialog.show();
                        } else {
                            NoTimeDialog dialog = new NoTimeDialog(mAc, "无法删除，此等级存在会员！");
                            dialog.setCanceledOnTouchOutside(false);
                            dialog.show();
                        }
                    }
                });
                idtvupdate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (item.isIs_edit()) {
                            Intent intent = new Intent(mAc, UpdateMemberLevelActivity.class);
                            intent.putExtra(UpdateMemberLevelActivity.MEMBER_LEVEL_ID, item.getId());
                            startActivityForResult(intent, 102);
                        }
                    }
                });
            }
        };
        contentView.setAdapter(adapter);
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.MEMBER_LEVEL_LIST)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        dismissProgressDialog();
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            Gson gson = new Gson();
                            MemberLevelModel memberLevelModel = gson.fromJson(json, MemberLevelModel.class);
                            list.clear();
                            list.addAll(memberLevelModel.getData().getList());
                            adapter.notifyDataSetChanged();
                            if (list.size() == 0) {
                                idEmptyLayout.setVisibility(View.VISIBLE);
                            } else {
                                idEmptyLayout.setVisibility(View.GONE);
                            }

                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showContent();
                            }

                        } else {
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showError();
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        dismissProgressDialog();
                        if (idMultipleStatusView != null) {
                            idMultipleStatusView.showError();
                        }
                    }
                });
    }


    private void delMember(String levelid) {
        HttpRequest.postUrl(Api.DEL_MEMBER_LEVEL)
                .addParams("id", levelid)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        dismissProgressDialog();
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            initHttp();
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        dismissProgressDialog();
                    }
                });
    }


    @Override
    public void getString(String content) {
        showProgressDialog();
        delMember(content);
    }
}
